﻿using System.Windows;
using System.Windows.Media;

namespace LogInModule.Views.Helpers
{
    public static class Extensions
    {
        public static void EnsureTransforms(this UIElement child)
        {
            ScaleTransform scale;
            SkewTransform skew;
            RotateTransform rotate;
            TranslateTransform translate;

            child.EnsureTransforms(out scale, out rotate, out skew, out translate);
        }

        public static void EnsureTransforms(this UIElement child, out ScaleTransform scale, out RotateTransform rotate,
                                            out SkewTransform skew, out TranslateTransform translate)
        {
            TransformGroup group = child.RenderTransform as TransformGroup ?? new TransformGroup();

            if (group.Children.Count < 4)
            {
                scale = new ScaleTransform();
                skew = new SkewTransform();
                rotate = new RotateTransform();
                translate = new TranslateTransform();

                group.Children.Add(scale);
                group.Children.Add(skew);
                group.Children.Add(rotate);
                group.Children.Add(translate);
            }
            else
            {
                scale = group.Children[0] as ScaleTransform;
                skew = group.Children[1] as SkewTransform;
                rotate = group.Children[2] as RotateTransform;
                translate = group.Children[3] as TranslateTransform;
            }
            //child.RenderTransformOrigin = new Point(.5, .5);
            child.RenderTransform = group;
        }

        public static void EnsureProjection(this UIElement child, out PlaneProjection projection)
        {
            projection = child.Projection as PlaneProjection ?? new PlaneProjection();

            child.Projection = projection;
        }
    }
}
